package com.phospherus.common.controller;

import com.phospherus.common.AttachedFile;
import com.phospherus.common.conf.UpLoadConf;
import com.phospherus.common.controller.handler.ResultHandler;
import com.phospherus.common.service.IAttachedFileService;
import org.apache.commons.io.FileUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;

/**
 * @author songjinlong
 * @version 1.0
 * @since 2017/12/20
 */
@Controller()
@RequestMapping("/common")
public class AttachedFileController extends BaseController<AttachedFile, IAttachedFileService> {
    @Autowired
    UpLoadConf upLoadConf;

    @RequestMapping(value = "/download")
    public ResponseEntity download2(long id) throws IOException {
        if (id < 1) {
            throw new RuntimeException("附件id为空");
        }
        AttachedFile attachedFile = service.findOne(id);
        if (attachedFile == null) {
            throw new RuntimeException("为找到附件");
        }
        String realFileName = attachedFile.getRealFileName();
        String fileName = attachedFile.getFileName();
        String filePath = upLoadConf.getBaseDirectory() + File.separator + fileName;
        HttpHeaders httpHeaders = new HttpHeaders();
        httpHeaders.setContentDispositionFormData("attachment", new String(realFileName.getBytes("UTF-8"), "ISO8859-1"));
        httpHeaders.setContentType(MediaType.APPLICATION_OCTET_STREAM);
        byte[] bytes = FileUtils.readFileToByteArray(new File(filePath));
        return new ResponseEntity(bytes, httpHeaders, HttpStatus.CREATED);
    }

    @RequestMapping(value = "/upload", method = RequestMethod.POST)
    @ResponseBody
    public ResultHandler upload2(MultipartFile file) {
        if (file == null) {
            return new ResultHandler(0, "file is null");
        }
        String realFileName = file.getOriginalFilename();
        String fileName = "temp" + File.separator + System.currentTimeMillis() + "_" + realFileName;
        String filePath = upLoadConf.getBaseDirectory() + File.separator + fileName;
        File outFile = new File(filePath);
        try {
            byte[] fileData = file.getBytes();
            FileUtils.writeByteArrayToFile(outFile, fileData);
            return new ResultHandler(ResultHandler.SystemMessage.SUCCESS, service.addNewAttacheFile(fileName, realFileName));
        } catch (IOException e) {
            e.printStackTrace();
        }
        return new ResultHandler(ResultHandler.SystemMessage.FAIL);
    }

    @RequestMapping("/delete")
    @ResponseBody
    public ResultHandler delete(Long id) {
        service.delete(id);
        return new ResultHandler(ResultHandler.SystemMessage.SUCCESS);
    }
}
